package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.event.WorkPageRefreshEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.RatingBar;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 确认工单--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class WorkConfirmActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, TextWatcher, RatingBar.OnRatingChangeListener, RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.rb_comfirm_left)
    RadioButton rbComfirmLeft;
    @Bind(R.id.rb_comfirm_right)
    RadioButton rbComfirmRight;
    @Bind(R.id.rg_comfirm)
    RadioGroup rgComfirm;
    @Bind(R.id.rtb_star_fives)
    RatingBar rtbStarFives;
    @Bind(R.id.tv_level)
    TextView tvLevel;
    @Bind(R.id.rl_level)
    LinearLayout rlLevel;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    private String serverGrade;//评分
    private String nowStat = "1";
    private String requestId;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_work);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        requestId = getIntent().getStringExtra("requestId");

        initData();
    }

    private void initData() {
        simpleTileView.setTitle("确认工单");
        TextView titleView = simpleTileView.getTitleView();
        Drawable titleLeft =getResources().getDrawable(R.mipmap.icon_title_help);
        titleLeft.setBounds(0, 0, titleLeft.getMinimumWidth(), titleLeft.getMinimumHeight());
        titleView.setCompoundDrawables(null, null, titleLeft, null);
        titleView.setCompoundDrawablePadding(10);

        titleView.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        etInputContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100), AppHelper.emojiFilter});
        etInputContent.addTextChangedListener(this);
        rtbStarFives.setClickable(true);
        rtbStarFives.setOnRatingChangeListener(this);
        rgComfirm.setOnCheckedChangeListener(this);

        rbComfirmLeft.toggle();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if ("1".equals(nowStat) && TextUtils.isEmpty(serverGrade)) {
                    showCenterInfoMsg("请选择评分");
                    return;
                }
                String evaluateView = etInputContent.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                if ("1".equals(companyType)) {//服务商
                    map.put("ownerCode", ownerBean.getRows().getOwnerCode());
                    map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                } else if ("2".equals(companyType)) {//业主
                    map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
                    map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                }
                map.put("requestId", requestId);
                map.put("serverGrade", "0".equals(nowStat) ? "" : serverGrade);
                map.put("nowStat", nowStat);
                map.put("evaluateView", evaluateView);
                ServiceRequestManager.getManager().setWorkConfirm(this, JsonMapUtil.mapToJson(map), this);
                break;
            case R.id.option_textview_title:
                GuideWebActivity.openActivity(this, Api.$().GUIDE_CONFIRM_WORK_URI);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
        switch (id) {
            case R.id.rb_comfirm_left:
                rlLevel.setVisibility(View.VISIBLE);
                tvInputTitle.setText("评价内容");
                nowStat = "1";
                break;
            case R.id.rb_comfirm_right:
                rlLevel.setVisibility(View.GONE);
                tvInputTitle.setText("未完成理由");
                nowStat = "0";
                break;
                default:
                break;
        }
    }

    @Override
    public void onRatingChange(float ratingCount) {
        int count = (int) ratingCount;
        serverGrade = count + "";
        switch (count) {
            case 1:
                tvLevel.setText("一星");
                break;
            case 2:
                tvLevel.setText("两星");
                break;
            case 3:
                tvLevel.setText("三星");
                break;
            case 4:
                tvLevel.setText("四星");
                break;
            case 5:
                tvLevel.setText("五星");
                break;
                default:
                break;
        }

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length = s.length();
        tvInputNumber.setText(length + "");
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                CommonBean bean = new Gson().fromJson(result, CommonBean.class);
                if (bean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    finish();
                    EventBus.getDefault().post(new WorkPageRefreshEvent(true));
                } else{
                    EventBus.getDefault().post(new WorkPageRefreshEvent());
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
                break;
                default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }

    public static void OpenActivity(Context context, String requestId) {
        Intent i = new Intent(context, WorkConfirmActivity.class);
        i.putExtra("requestId", requestId);
        context.startActivity(i);
    }

}
